//
//  ZPComposeToolbar.swift
//  Weibo
//
//  Created by 张鹏 on 16/7/3.
//  Copyright © 2016年 张鹏. All rights reserved.
//

import UIKit
//toolbar 按钮枚举
enum ZPComposeToolbarType: Int{
    //图片
    case Picture = 10
    //@
    case Mention = 20
    //# 
    case Trend = 30
    //表情
    case Emoticon = 40
    //+
    case Add = 50
}




class ZPComposeToolbar: UIView {
    
    //告知外界按钮点击啦
    var closure:((type: ZPComposeToolbarType)->())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: -  设置视图
    private func setupUI(){
        backgroundColor = UIColor.whiteColor()
        //添加控件
        let pictureButton = addChildButtons("compose_toolbar_picture",type: .Picture)
        let mentionButton = addChildButtons("compose_mentionbutton_background",type: .Mention)
        let trendButton = addChildButtons("compose_trendbutton_background",type: .Trend)
        let emoticonButton = addChildButtons("compose_emoticonbutton_background",type: .Emoticon)
        let addButton = addChildButtons("compose_add_background",type: .Add)
        //添加约束
        pictureButton.snp_makeConstraints { (make) in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(mentionButton)
        }
        mentionButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(pictureButton.snp_trailing)
            make.width.equalTo(trendButton)
        }
        trendButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(mentionButton.snp_trailing)
            make.width.equalTo(emoticonButton)
        }
        emoticonButton.snp_makeConstraints { (make) in
            make.top.bottom.equalTo(self)
            make.leading.equalTo(trendButton.snp_trailing)
            make.width.equalTo(addButton)
        }
        
        addButton.snp_makeConstraints { (make) in
            make.top.bottom.trailing.equalTo(self)
            make.leading.equalTo(emoticonButton.snp_trailing)
        }
        
    }
    // MARK: - 监听方法
    func buttonClick(button: UIButton){
        closure?(type:ZPComposeToolbarType(rawValue: button.tag)!)
    }
    //创建button按钮的公共方法
    func addChildButtons(imgName: String, type: ZPComposeToolbarType) -> UIButton{
        let button = UIButton()
        // rawValue 枚举值.rawValue ==  Int
        button.tag = type.rawValue
        //添加点击事件
        button.addTarget(self, action: #selector(ZPComposeToolbar.buttonClick(_:)), forControlEvents: UIControlEvents.TouchUpInside)
        button.setImage(UIImage(named: imgName), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "\(imgName)__highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: UIControlState.Highlighted)
        addSubview(button)
        return button
    }

}
